package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.db.PageResult;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.VisitDto;
import com.zzyl.entity.Elder;
import com.zzyl.entity.User;
import com.zzyl.entity.Visit;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ElderMapper;
import com.zzyl.mapper.VisitMapper;
import com.zzyl.service.ElderService;
import com.zzyl.service.VisitService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CheckInVo;
import com.zzyl.vo.VisitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.beancontext.BeanContext;
import java.lang.ref.PhantomReference;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class VisitServiceImpl implements VisitService {

    @Autowired
    private VisitMapper visitMapper;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 更新来访
     */
    @Override
    public void update(Long id, VisitDto dto) {
        Visit visit = visitMapper.findById(id);
        if (visit != null) {
            BeanUtils.copyProperties(dto, visit);
            visit.setId(id);
            visit.setUpdateTime(LocalDateTime.now());
            visitMapper.update(visit);
        }
    }

    /**
     * 取消来访
     */
    @Override
    public void cancelVisit(Long id) {
        Visit visit = visitMapper.findById(id);
        if (visit != null) {
            visit.setStatus(ReservationStatus.CANCELED.getOrdinal());
            visit.setUpdateTime(LocalDateTime.now());
            Long userId = UserThreadLocal.getMgtUserId();
            visit.setUpdateBy(userId);
            visitMapper.update(visit);
        }
    }

    /**
     * 根据id删除来访
     */
    @Override
    public void deleteById(Long id) {
        visitMapper.deleteById(id);
    }

    /**
     * 根据id查找来访
     */
    @Override
    public VisitVo findById(Long id) {
        Visit visit = visitMapper.findById(id);
        if (visit != null) {
            return convertToVO(visit);
        }
        return null;
    }


    /**
     * 查找所有来访
     * @param mobile 来访人手机号
     * @param time 来访时间
     */
    @Override
    public List<VisitVo> findAll(String mobile, LocalDateTime time) {
        // 计算24小时后的时间
        LocalDateTime endTime = time.plusHours(24);
        // 根据mobile和时间范围查询来访
        List<Visit> visits = visitMapper.findAll(mobile, time, endTime);
        return convertToVOList(visits);
    }

    /**
     * 将Visit转换为VisitVO
     */
    private VisitVo convertToVO(Visit visit) {
        return BeanUtil.toBean(visit, VisitVo.class);
    }

    /**
     * 将List<Visit>转换为List<VisitVO>
     */
    private List<VisitVo> convertToVOList(List<Visit> visits) {
        return visits.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    /*
      新增来访
     */
    @Override
    public void addVisit(VisitDto visitDto) {
        Visit visit = new Visit();
        BeanUtils.copyProperties(visitDto,visit);
        visit.setCreateTime(LocalDateTime.now());
        visit.setCreateBy(UserThreadLocal.getUserId());
        visit.setStatus(ReservationStatus.PENDING.getOrdinal());
        visitMapper.addVisit(visit);
    }

    /*
 来访信息分页查询
  */
    @Override
    public PageResponse pageVisit(Integer pageNum, Integer pageSize, LocalDateTime startTime, LocalDateTime endTime, Integer status, String name, String phone, Integer type) {
        PageHelper.startPage(pageNum,pageSize);

        List<VisitVo> visitVos = visitMapper.pageVisit(endTime,status,startTime,name,phone,type);

        Page<VisitVo> p = (Page<VisitVo>) visitVos;


        return PageResponse.of(p,VisitVo.class);
    }
}



